package com.milhouz.bomberrokon;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.milhouz.bomberrokon.anim.GalleryLayout;
import com.milhouz.bomberrokon.anim.HorizontalSlidingLayoutWithAnim;
import com.milhouz.bomberrokon.factories.MapFactory;
import com.milhouz.bomberrokon.model.MapModel;
import com.milhouz.bomberrokon.util.MapXmlReader;

public class MapSelectionActivity extends Activity {

	private HorizontalSlidingLayoutWithAnim layoutGallery;

	private String[] mMaps;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// Set the content view
		setContentView(R.layout.map_selection);

		// Load default Map Settings
		mMaps = MapFactory.getFactory().listMaps(getAssets());

		// Get the slider and add views
		layoutGallery = (HorizontalSlidingLayoutWithAnim) findViewById(R.id.layoutGallery);
		layoutGallery.MAX_CHILDREN = mMaps.length;
		if (mMaps.length % 2 == 1) {
			layoutGallery.NUMBER_CHILDREN_UNDER = Math.round(mMaps.length / 2) - 1;
		} else {
			layoutGallery.NUMBER_CHILDREN_UNDER = mMaps.length / 2;
		}

		ImageView map = null;
		GalleryLayout linearLayout = null;
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( //
				LinearLayout.LayoutParams.WRAP_CONTENT, //
				LinearLayout.LayoutParams.WRAP_CONTENT);
		int i = 0;
		String[] mapNames = new String[mMaps.length];
		for (String mapCode : mMaps) {
			linearLayout = new GalleryLayout(mapCode, getApplicationContext());
			linearLayout.setPadding(5, 5, 5, 5);
			linearLayout.setBackgroundColor(Color.WHITE);
			map = new ImageView(getApplicationContext());
			try {
				map.setBackgroundDrawable(Drawable.createFromStream(getAssets().open(
						MapXmlReader.ASSETS_MAP_FOLDER + "/" + mapCode + "/" + MapXmlReader.THUMB_FILENAME), mapCode));
			} catch (IOException e) {
				map.setBackgroundResource(R.drawable.default_thumb);
			}

			linearLayout.addView(map);
			layoutGallery.addView(linearLayout, layoutParams);
			// Get and load map meta data
			MapModel mapModel = MapXmlReader.getMapSettings(this, mapCode);
			mapNames[i++] = mapModel.getCompleteMapName();
		}
		layoutGallery.IMAGE_NAMES = mapNames;
		layoutGallery.IMAGE_NAME_TEXT_VIEW = (TextView) findViewById(R.id.mapNameText);
		layoutGallery.IMAGE_NAME_TEXT_VIEW.setText(mapNames[0]);
	}

}